﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace Jewelnoid
{
    class Menu : SceneBase
    {
        private InputController input;

        public SpriteFont spritefont;
        public Text2D arkz;
        public Text2D survival;
        public Text2D arcade;
        public Text2D credits;

        public MenuBackground background;

        public override void draw(SpriteBatch spritebatch)
        {
            background.draw(spritebatch);
            arkz.draw(spritebatch);
            survival.draw(spritebatch);
            arcade.draw(spritebatch);
            credits.draw(spritebatch);
        }

        public override void terminate()
        {
            survival = null;
            arcade = null;
            credits = null;
            input = null;
            background = null;

            SoundManager.pauseMusic();
        }

        public override void start()
        {
            this.spritefont = Game1.self.Content.Load<SpriteFont>("8BitWonder");

            Vector2 windowSize = new Vector2(Game1.self.graphics.PreferredBackBufferWidth, Game1.self.graphics.PreferredBackBufferHeight);
            Vector2 selectionTextScale = new Vector2(0.5f, 0.5f);

            String arkzText = "ARKZ";
            String survivalText = "Survival";
            String arcadeText = "Arcade";
            String creditsText = "Credits";

            this.input = new InputController();

            this.background = new MenuBackground();

            this.arkz = new Text2D(this.spritefont, arkzText, this.background.textArea1Image.position - this.spritefont.MeasureString(arkzText) / 2, Color.White);
            this.survival = new Text2D(this.spritefont, survivalText, this.background.textArea2Image.position - (this.spritefont.MeasureString(survivalText) * selectionTextScale) / 2, Color.White);
            this.arcade = new Text2D(this.spritefont, arcadeText, this.background.textArea2Image.position - (this.spritefont.MeasureString(arcadeText) * selectionTextScale) / 2, Color.Red);
            this.credits = new Text2D(this.spritefont, creditsText, this.background.textArea2Image.position - (this.spritefont.MeasureString(creditsText) * selectionTextScale) / 2, Color.White);

            this.arcade.position.Y -= ((this.spritefont.MeasureString(survivalText).Y * selectionTextScale.Y) + ((this.spritefont.MeasureString(arcadeText) * selectionTextScale).Y / 2));
            this.credits.position.Y += ((this.spritefont.MeasureString(survivalText).Y * selectionTextScale.Y) + ((this.spritefont.MeasureString(creditsText) * selectionTextScale).Y / 2));

            this.survival.scale = selectionTextScale;
            this.arcade.scale = selectionTextScale;
            this.credits.scale = selectionTextScale;

            if (SoundManager.isMusicPaused())
            {
                SoundManager.resumeMusic();
            }
            else
            {
                SoundManager.playMusic("arkanoid", true);
            }
        }

        public override void update(GameTime gameTime)
        {
            Boolean optionSelected = false;

            this.input.getState();

            this.background.update();

            if (this.input.isKeyJustPressed(Keys.Down))
            {
                changeOption(Keys.Down);
            }
            else if (this.input.isKeyJustPressed(Keys.Up))
            {
                changeOption(Keys.Up);
            }

            if (this.input.isMouseLeftButtonJustPressed(this.arcade))
            {
                changeOption(this.arcade);
                optionSelected = true;
            }

            if (this.input.isMouseLeftButtonJustPressed(this.survival))
            {
                changeOption(this.survival);
                optionSelected = true;
            }

            if (this.input.isMouseLeftButtonJustPressed(this.credits))
            {
                changeOption(this.credits);
                optionSelected = true;
            }

            if (this.input.isKeyJustPressed(Keys.Enter))
            {
                optionSelected = true;
            }

            if (optionSelected)
            {
                changeScreen();
            }
        }

        public void changeScreen()
        {
            if (arcade.color == Color.Red)
            {
                SceneManager.setScene(new LevelArcade());
            }
            else if (survival.color == Color.Red)
            {
                SceneManager.setScene(new LevelSurvival());
            }
            else if (credits.color == Color.Red)
            {
                SceneManager.setScene(new Credits());
            }
        }

        public void changeOption(Text2D text)
        {
            if (text == arcade)
            {
                changeAllOptionsToWhite();
                arcade.color = Color.Red;
            }
            else if (text == survival)
            {
                changeAllOptionsToWhite();
                survival.color = Color.Red;
            }
            else if (text == credits)
            {
                changeAllOptionsToWhite();
                credits.color = Color.Red;
            }
        }

        public void changeOption(Keys key)
        {
            if (key == Keys.Down)
            {
                if (arcade.color == Color.Red)
                {
                    changeAllOptionsToWhite();
                    survival.color = Color.Red;
                }
                else if (survival.color == Color.Red)
                {
                    changeAllOptionsToWhite();
                    credits.color = Color.Red;
                }
                else
                {
                    changeAllOptionsToWhite();
                    arcade.color = Color.Red;
                }
            }
            else if (key == Keys.Up)
            {
                if (arcade.color == Color.Red)
                {
                    changeAllOptionsToWhite();
                    credits.color = Color.Red;
                }
                else if (survival.color == Color.Red)
                {
                    changeAllOptionsToWhite();
                    arcade.color = Color.Red;
                }
                else
                {
                    changeAllOptionsToWhite();
                    survival.color = Color.Red;
                }
            }
        }

        private void changeAllOptionsToWhite()
        {
            arcade.color = Color.White;
            survival.color = Color.White;
            credits.color = Color.White;
        }
    }
}
